<template>
    <view class="main dispaly-col-center">
        <view class="box dispaly-col-center">
            <view class="logo">
                <image src="/static/test.jpg" mode="aspectFill" />
            </view>
            <view v-if="loginType" class="dispaly-col-center">
                <view class="tel">156****3575</view>
                <view class="btn-box1">
                    <button class="btn1 b" v-if="proxy" open-type="getPhoneNumber"
                        @getphonenumber="getphonenum">一键登录</button>
                    <view class="btn1 b" v-else @click="login">一键登录</view>
                    <view class="btn1 n-b" @click="loginType = false">使用其他手机登录</view>
                </view>
            </view>

            <view class="tel-login dispaly-col-center" v-else>
                <view class="form">
                    <view class="form-item">
                        <input type="number" placeholder="请输入手机号" maxlength="11" class="t">
                    </view>
                    <view class="form-item">
                        <input type="number" placeholder="请输入验证码" maxlength="6" class="c">
                        <view class="code">
                            <view class="get-code" v-if="show" @click="getCode">获取验证码</view>
                            <view class="get-code" v-else>{{ count + 's后获取' }}</view>
                        </view>
                    </view>
                </view>
                <view class="btn-box1">
                    <view class="btn1 b" @click="telLogin">登录</view>
                </view>
                <view class="back" @click="loginType = true">返回一键登录</view>
            </view>

        </view>
        <view class="proxy w-100 dispaly-align-center">
            <view @click="proxy = !proxy" class="q dispaly-align-center">
                <view class="p" v-if="proxy"></view>
            </view>
            <view class="txt">登录即代表已同意 <text class="a"
                    @click="navTo(`/subPackage/mine/setting/agreement?type=${1}`)">服务协议，</text> <text class="a"
                    @click="navTo(`/subPackage/mine/setting/agreement?type=${2}`)">隐私政策</text></view>
        </view>
    </view>
</template>

<script setup lang="ts">
import { navTo } from '@/utils/util'
import { reactive, ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { debounce } from '@/utils/util';

onShow(() => {
    uni.login({
        success: (res) => {
            console.log(res, 'res')
            formData.wxCode = res.code;
        }
    });
})

let show = ref<boolean>(true)
let count = ref<number | any>()
let timer = ref<any>(null)
// 获取验证码
let ok = ref<boolean>(true)
const getCode = debounce(
    () => {
        if (ok.value) {
            uni.showToast({
                title: '验证码已发送请注意查收',
                icon: 'none'
            })
            const TIME_COUNT = 60;
            if (!timer.value) {
                count.value = TIME_COUNT;
                show.value = false;
                timer.value = setInterval(() => {
                    if (count.value > 0 && count.value <= TIME_COUNT) {
                        count.value -= 1;
                    } else {
                        show.value = true;
                        clearInterval(timer.value);
                        timer.value = null;
                    }
                }, 1000);
            }
        } else {
            uni.showToast({
                title: '验证码发送失败',
                icon: 'none'
            })
        }

    },
    1500,
    false
);

let proxy = ref<boolean>(false)
let loginType = ref<boolean>(true)

interface L {
    wxCode: string,
    telCode: string
}
let formData = reactive<L>({
    wxCode: '',
    telCode: ''
})

const getphonenum = (e: any) => {
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
        formData.telCode = e.detail.code
        uni.showLoading({
            mask: true,
            title: '登录中...'
        });

    }
};

const login = async () => {
    if (!proxy.value) {
        uni.showToast({
            title: '请先同意协议！',
            icon: 'none'
        })
    }
}

const telLogin = async () => {
    if (!proxy.value) {
        uni.showToast({
            title: '请先同意协议！',
            icon: 'none'
        })
    } else {
        console.log('ok')
    }
}


</script>

<style>
page {
    height: 100vh;
    background: rgb(255, 255, 255);
    background: linear-gradient(0deg, rgba(255, 255, 255, 1) 0%, rgba(169, 231, 233, 1) 100%);
}
</style>

<style lang="scss" scoped>
.btn1 {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 612rpx;
    height: 98rpx;
    border-radius: 12rpx 12rpx 12rpx 12rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 28rpx;
}

.b {
    background: #008298;
    color: #FFFFFF;
}

.n-b {
    color: #008298;
    border: 2rpx solid #008298;
}

.tel-login {
    .back {
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 26rpx;
        color: #B5B5B5;
        color: #008298;
    }

    .form {
        margin-top: 166rpx;

        .form-item:first-child {
            margin-bottom: 48rpx;
        }

        .form-item {
            width: 612rpx;
            height: 98rpx;
            background: #FFFFFF;
            opacity: 0.66;
            border-radius: 12rpx;
            position: relative;

            .code {
                position: absolute;
                right: 51rpx;
                top: 50%;
                transform: translate(0, -50%);

                .get-code {
                    font-family: PingFang SC, PingFang SC;
                    font-weight: 400;
                    font-size: 28rpx;
                    color: #008298;
                }
            }

            input {
                width: 612rpx;
                height: 98rpx;
                background: #FFFFFF;
                opacity: 0.66;
                padding: 0 24rpx;
            }
        }
    }
}

.main {
    height: 100vh;

    .box {
        .logo {
            image {
                width: 174rpx;
                height: 174rpx;
                border-radius: 12rpx;
            }
        }

        .btn-box1 {
            margin-top: 102rpx;

            .btn1:first-child {
                margin-bottom: 40rpx;
            }
        }

        .tel {
            font-family: PingFang SC, PingFang SC;
            font-weight: 800;
            font-size: 48rpx;
            color: #485658;
            margin-top: 187rpx;
        }
    }
}

.proxy {
    position: fixed;
    bottom: 73rpx;
    left: 0;
    right: 0;

    .q {
        width: 27rpx;
        height: 27rpx;
        border-radius: 50%;
        border: 1rpx solid #008298;
        margin-right: 10rpx;

        .p {
            width: 20rpx;
            height: 20rpx;
            background-color: #008298;
            border-radius: 50%;
        }
    }

    .txt {
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 24rpx;
        color: #B5B5B5;

        .a {
            color: #008298;

        }
    }
}
</style>